<template>
  <!-- 用户上传分析 -->
  <div id="space-user-analyze">
    <a-card title="用户上传分析">
      <v-chart :option="option" style="height: 320px" :loading="loading"></v-chart>
      <template v-slot:extra>
        <a-space>
          <a-segmented v-model:value="timeDimension" :options="timeDimensionOptions" />
          <a-input-search
            placeholder="请输入用户id"
            enter-button="搜索用户"
            @search="doSearch"
          />
        </a-space>
      </template>
    </a-card>
  </div>
</template>

<script setup lang="ts">
import { ref, withDefaults, defineProps, onMounted, computed } from 'vue'
import {
  getSpaceUserAnalyzeUsingPost,
} from '@/request/api/spaceAnalyzeController.ts'
import { message } from 'ant-design-vue'
import 'echarts-wordcloud'

interface Props {
  queryAll?: boolean
  queryPublic?: boolean
  spaceId?: number
}

const props = withDefaults(defineProps<Props>(), {
  queryAll: false,
  queryPublic: false,
})
const userId = ref<string>()
const dataList = ref<API.SpaceUserAnalyzeResponse[]>([])
const loading = ref(false)
const timeDimension = ref('day')
const timeDimensionOptions = [
  {
    label: '天',
    value: 'day',
  },
  {
    label: '周',
    value: 'week',
  },
  {
    label: '月',
    value: 'month',
  },
]

const fetchData = async () => {
  loading.value = true
  const { data: res } = await getSpaceUserAnalyzeUsingPost({
    queryAll: props.queryAll,
    queryPublic: props.queryPublic,
    spaceId: props.spaceId,
    timeDimension: timeDimension.value,
    userId: userId.value,
  })
  if (res.code === 0 && res.data) {
    dataList.value = res.data
  } else {
    message.error('获取数据失败,' + res.message)
  }
  loading.value = false
}
const option = computed(() => {
  const periods = dataList.value.map((item) => item.period) // 时间区间
  const counts = dataList.value.map((item) => item.count) // 上传数量

  return {
    tooltip: { trigger: 'axis' },
    xAxis: { type: 'category', data: periods, name: '时间区间' },
    yAxis: { type: 'value', name: '上传数量' },
    series: [
      {
        name: '上传数量',
        type: 'line',
        data: counts,
        smooth: true, // 平滑折线
        emphasis: {
          focus: 'series',
        },
      },
    ],
  }
})

const doSearch = (value: string) => {
  userId.value = value
  fetchData()
}

onMounted(() => {
  fetchData()
})
</script>

<style lang="less" scoped></style>
